//
//  oldBannerView.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/23.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldBannerView.h"
#import "SDCycleScrollView.h"
#import "oldNetWork.h"
#import "oldHub.h"

@interface oldBannerView()<SDCycleScrollViewDelegate>
@property (nonatomic, strong)SDCycleScrollView *scrollView;
@property (nonatomic, copy)NSArray *dataSource;
@end

@implementation oldBannerView

- (void)initBanner
{
    _scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake( 0, 0, SCREEN_WIDTH, 198 ) delegate:self placeholderImage:nil];
//    if ( Screen_Width < 500 ) {//手机端：包括iPhone8
//        _scrollView.frame = CGRectMake( 0, 0, Screen_Width, Screen_Width / 320 * 100);
//    }
    _scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _scrollView.currentPageDotColor = OLD_COLOR;
    _scrollView.pageDotColor = [UIColor whiteColor];
    _scrollView.autoScrollTimeInterval = 5.0f;
    [self addSubview:_scrollView];
    
    [self loadBannerUrl];
}

- (void)loadBannerUrl
{
    [OLDNetWork httpRequestWithPostPut:URL_GetBannerList params:nil success:^(id data) {
        [self initArraywithData:data];
    } error:^(NSString *error) {
        [oldHub warningWindow:error];
    }];
}

- (void)initArraywithData:(NSArray *)array
{
    _dataSource = array;
    NSMutableArray *imglist = [NSMutableArray array];
    
    for (int i = 0; i < [array count]; i++) {
        [imglist addObject:array[i][@"pic"]];
    }
    
    _scrollView.imageURLStringsGroup = imglist;
}

- ( void ) cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    id data = _dataSource[index];
    
    [self.delegate bannerClicked:data[@"linkPath"] Type:[data[@"linkType"] integerValue]];
}

@end
